package my.myaction.tool.core;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;
import java.text.SimpleDateFormat;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-10-20
 * Time: 9:29:34
 * To change this template use File | Settings | File Templates.
 */
public class FormBean {
    private Map<String, String> lowerCaseMap = new HashMap<String, String>();
    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public FormBean(HttpServletRequest request) {
        if (request != null) {
            Map<String, String[]> paramMap = request.getParameterMap();
            Iterator it = paramMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                String key = (String) entry.getKey();
                Object value = entry.getValue();
                String valueStr = "";
                if (value == null) {
                    valueStr = "";
                } else if (value instanceof Object[]) {
                    Object[] objarr = (Object[]) value;
                    if (objarr.length <= 0) {
                        valueStr = "";
                    } else if (objarr.length == 1) {
                        valueStr = objarr[0].toString();
                    } else {
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < objarr.length; i++) {
                            if (sb.length() > 0) sb.append(",");
                            sb.append(objarr[i].toString());
                        }
                        valueStr = sb.toString();
                    }
                } else {
                    valueStr = value.toString();
                }
                lowerCaseMap.put(key.toLowerCase(), valueStr);
            }
        }
    }

    public FormBean(Map<String, Object> paramMap) {
        if (paramMap != null) {
            Iterator it = paramMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                String key = (String) entry.getKey();
                Object value = entry.getValue();
                String valueStr = "";
                if (value == null) {
                    valueStr = "";
                } else if (value instanceof Object[]) {
                    Object[] objarr = (Object[]) value;
                    if (objarr.length <= 0) {
                        valueStr = "";
                    } else if (objarr.length == 1) {
                        valueStr = objarr[0].toString();
                    } else {
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < objarr.length; i++) {
                            if (sb.length() > 0) sb.append(",");
                            sb.append(objarr[i].toString());
                        }
                        valueStr = sb.toString();
                    }
                } else {
                    valueStr = value.toString();
                }
                lowerCaseMap.put(key.toLowerCase(), valueStr);
            }
        }
    }

    public Map<String, String> getLowerCaseMap() {
        return lowerCaseMap;
    }

    public String getString(String s) {
        if (s == null) return "";
        String value = lowerCaseMap.get(s.toLowerCase());
        if (value == null) return "";
        return value;
    }

    public int getInt(String s) {
        String value = getString(s);
        int rt = 0;
        try {
            rt = new Integer(value);
        } catch (Exception ex) {
            rt = 0;
        }
        return rt;
    }

    public short getShort(String s) {
        String value = getString(s);
        short rt = 0;
        try {
            rt = new Short(value);
        } catch (Exception ex) {
            rt = 0;
        }
        return rt;
    }

    public long getLong(String s) {
        String value = getString(s);
        long rt = 0;
        try {
            rt = new Long(value);
        } catch (Exception ex) {
            rt = 0;
        }
        return rt;
    }

    public float getFloat(String s) {
        String value = getString(s);
        float rt = 0;
        try {
            rt = new Float(value);
        } catch (Exception ex) {
            rt = 0;
        }
        return rt;
    }

    public double getDouble(String s) {
        String value = getString(s);
        double rt = 0;
        try {
            rt = new Double(value);
        } catch (Exception ex) {
            rt = 0;
        }
        return rt;
    }

    public byte getByte(String s) {
        String value = getString(s);
        byte rt = 0;
        try {
            rt = new Byte(value);
        } catch (Exception ex) {
            rt = 0;
        }
        return rt;
    }

    public boolean getBoolean(String s) {
        String value = getString(s);
        boolean rt = false;
        try {
            rt = new Boolean(value);
        } catch (Exception ex) {
            rt = false;
        }
        return rt;
    }

    public Date getDate(String s) {
        String value = getString(s);
        if (value.equals("")) return null;
        if (value.indexOf(" ") <= -1) {//只有日期或时间
            if (value.indexOf("-") > -1) {//日期
                value += " 00:00:00";
            } else if (value.indexOf(":") > -1) {//时间
                value = "1970-01-01 " + value;
            } else {
                return null;
            }
        }
        Date rt = null;
        try {
            rt = formatter.parse(value);
        } catch (Exception ex) {
            rt = null;
        }
        return rt;
    }


    public static void main(String[] args){
        System.out.println("".split(",").length);
    }

}
